/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.lib.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import org.bff.javampd.MPDPlaylist;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDPlaylistException;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author Bill
 */
public abstract class PlaylistMoveAction extends AbstractAction {

    private MPDPlaylist mpdPlaylist;
    
    public PlaylistMoveAction(MPDPlaylist playlist) {
        this.mpdPlaylist = playlist;
    }

    public void swapSongs(MPDSong song1, MPDSong song2) throws MPDPlaylistException, MPDConnectionException {
        List musicList = new ArrayList(mpdPlaylist.getSongList());
        
        MPDSong mpdSong1 = null;
        MPDSong mpdSong2 = null;

        for (Iterator iter = musicList.iterator(); iter.hasNext();) {
            MPDSong mpdSong = (MPDSong) iter.next();
            if (mpdSong.getFile().equals(song1.getFile())) {
                mpdSong1 = mpdSong;
            }

            if (mpdSong.getFile().equals(song2.getFile())) {
                mpdSong2 = mpdSong;
            }

            if (mpdSong1 != null && mpdSong2 != null) {
                break;
            }
        }
        
        try {
            mpdPlaylist.swap(mpdSong1, mpdSong2);
        } catch (MPDConnectionException ex) {
            Logger.getLogger(PlaylistMoveAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDPlaylistException ex) {
            Logger.getLogger(PlaylistMoveAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
